/*
 * NewJFrame.java
 *
 * Created on 30 maj 2007, 10:55
 */

/**
 *
 * @author  Rufjan
 */
package gui;

import engine.*;
import util.*;
import javax.swing.*;

public class MainWindow extends javax.swing.JFrame {
    
    /** Creates new form NewJFrame */
    public MainWindow( WormMemory mem ) {
    	memory = mem;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                          
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jTextField1.setText("search.......");

        jButton1.setText("Search");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SearchListener(evt);
            }
        });

        jTable1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null}
            },
            new String [] {
                "Address"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        
        pack();
    }// </editor-fold>                        

    private void SearchListener(java.awt.event.ActionEvent evt) {                                
// TODO add your handling code here:
        System.out.println("SEARCH, SEARCH, SEARCH");
        Object[] res = memory.query( jTextField1.getText() );
        
        if( res!= null ) {
        	String[][] tab = new String[res.length][1];
        	for( int i=0; i<res.length; ++i ) {
        		tab[i][0] = ((LinkAndPriority)res[i]).link;
        	}
        	jTable1.setModel(new javax.swing.table.DefaultTableModel(
        		tab,
                new String [] {
                   "Links connected with " + jTextField1.getText()
                }
            ) {
                boolean[] canEdit = new boolean [] {
                    false, false, false
                };

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit [columnIndex];
                }
            });
        }
        else { 
        	JOptionPane.showConfirmDialog(
        		this, 
        		"QUERY NOT FOUND!", 
        		"QUERY NOT FOUND!", 
        		JOptionPane.DEFAULT_OPTION,
        		JOptionPane.WARNING_MESSAGE 
        	);
        }
    }                               
    
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   
    private WormMemory memory;
}
